var DeptInfoDlg = {
    data: {
        pid: "",
        pName: ""
    }
};
layui.use(['layer', 'form', 'upload', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var upload = layui.upload;
    var admin = layui.admin;
    var layer = layui.layer;

    // 让当前iframe弹层高度适应
    admin.iframeAuto();

    $('#partnerDiv').children("input").val('');
    //获取城市合伙人信息
    var ajax = new $ax(Feng.ctxPath + "/dept/detail/" + Feng.getUrlParam("deptId"), function (data) {
        console.log(data)
        if ((data && data.deptId == 10001) || (data && data.pid == 10001)) {
            $("#partnerDiv").show();
        } else {
            $("#partnerDiv").hide();
        }
    });
    var result = ajax.start();
    form.val('deptForm', result);


    function uploadInit(id, inputId, options) {
        //是否累加
        var accumulation = options ? options.accumulation : false;
        var elem = '#' + id;
        var input = '#' + inputId;
        if(accumulation){
            $(input).val('');
        }
        var p = {
            elem: elem,
            accept: 'images',
            url: '/contest/cus/uploadLogo', // 上传接口
            choose: function () {
                $(elem).text('上传中');
            },
            done: function (res) {
                // 上传完毕回调
                if (res.code == 200) {
                    var logo = $(input).val();
                    if (logo) {
                        if (accumulation) {
                            logo += ";" + res.data;
                        } else {
                            logo = res.data;
                        }
                    } else {
                        logo = res.data;
                    }
                    $(input).val(logo);
                    $(elem).text('上传成功');

                } else {
                    Feng.error("上传失败!");
                    $(elem).text('上传失败');
                }
            },
            error: function (e) {
                // 请求异常回调
                $(elem).text('上传失败');
                Feng.error("上传失败!");
            }
        };
        $.extend(p, options);
        upload.render(p);
    }

    // 点击上级时
    $('#pName').click(function () {
        var formName = encodeURIComponent("parent.DeptInfoDlg.data.pName");
        var formId = encodeURIComponent("parent.DeptInfoDlg.data.pid");
        var treeUrl = encodeURIComponent("/dept/childTree");
        layer.open({
            type: 2,
            title: '父级城市合伙人',
            area: ['300px', '200px'],
            content: Feng.ctxPath + '/system/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
            end: function () {
                var pid = $("#pid").val(DeptInfoDlg.data.pid);
                if (DeptInfoDlg.data.pid && DeptInfoDlg.data.pid == 10001) {
                    $("#partnerDiv").show();
                    admin.iframeAuto();
                } else {
                    admin.iframeAuto();
                    $("#partnerDiv").hide();
                    $('#partnerDiv').children("input").val('');
                }
                $("#pName").val(DeptInfoDlg.data.pName);
            }
        });
    });
    uploadInit('editLogo', 'deptLogo');
    uploadInit('qrCodeUpload', 'qrCode');
    uploadInit('assistLogoUpload', 'assistLogo', {multiple: true, accumulation: true});

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/dept/update", function (data) {
            Feng.success("修改成功！");

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("修改失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();
    });
});